/**
 * @description:
 * @author: qintao
 * @create: 2019-08-23 16:04
 **/
package com.guoranhao.web.controller.base;

import com.alibaba.fastjson.JSON;
import com.guoranhao.tool.constants.BaseErrorCodeEnum;
import com.guoranhao.tool.utils.FileUploadUtil;
import com.guoranhao.tool.utils.LogUtil;
import com.guoranhao.tool.vo.MessageVo;
import com.guoranhao.web.util.SplicePathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * Description:  文件上传<br/>
 * Version V1.0  2019-8-23 16:53  by 秦涛   (qintao@cloud-young.com) 创建
 */
@RestController
@RequestMapping("/file/")
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);


    /**
     * Description:  上传文件 post formData方式<br/>
     * Version V1.0  2019-8-23 17:14  by 秦涛   (qintao@cloud-young.com) 创建
     * @param file 文件
     * @return 路径
     */
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public MessageVo<String> uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) {
        if (file != null) {
            try {
                return FileUploadUtil.uploadImg2OSSDefault(1, file.getInputStream());
            } catch (Exception e) {
                LogUtil.error(logger, e, "【getIntroduceList】error introduceListParam:{0}", Objects.nonNull(file) ? JSON.toJSONString(file) : "");
            }
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100003.getCode(),BaseErrorCodeEnum.ERRCODE_100003.getMsg());
    }

    /**
     * Description:  图片路径拼接示例<br/>
     * Version V1.0  2019-8-23 18:14  by 秦涛   (qintao@cloud-young.com) 创建
     * @return 完整路径
     */
    @RequestMapping(value = "getImage",method={RequestMethod.POST, RequestMethod.GET})
    public MessageVo<String> getImage(){

        return new MessageVo<>(false, SplicePathUtil.spliceUrl("guoranhaoshop/20190823100412-29c37444591e854dc91a4450bd21771c.png"));
    }


}
